{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# KFServing Pipeline samples\n",
    "\n",
    "This notebook uses an older version of the KFServing Pipelines component meant for clusters using a KFServing version less than v0.5.0 which only supports the v1alpha2 API."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Install the necessary kfp library"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip3 install kfp --upgrade"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import kfp.dsl as dsl\n",
    "import kfp\n",
    "from kfp import components\n",
    "import json\n",
    "\n",
    "# Create kfp client\n",
    "# Note: Add the KubeFlow Pipeline endpoint below if the client is not running on the same cluster.\n",
    "client = kfp.Client('kfserving_endpoint')\n",
    "EXPERIMENT_NAME = 'KFServing Experiments'\n",
    "experiment = client.create_experiment(name=EXPERIMENT_NAME)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### TensorFlow example\n",
    "\n",
    "Note: Change the action from `update` to `create` if you are deploying the model for the first time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "kfserving_op = components.load_component_from_url(\n",
    "  'https://raw.githubusercontent.com/kubeflow/pipelines/65bed9b6d1d676ef2d541a970d3edc0aee12400d/components/kubeflow/kfserving/component.yaml'\n",
    ")\n",
    "\n",
    "@dsl.pipeline(\n",
    "  name='kfserving pipeline',\n",
    "  description='A pipeline for kfserving.'\n",
    ")\n",
    "def kfservingPipeline(\n",
    "    action = 'update',\n",
    "    model_name='tf-sample',\n",
    "    default_model_uri='gs://kfserving-samples/models/tensorflow/flowers',\n",
    "    canary_model_uri='gs://kfserving-samples/models/tensorflow/flowers-2',\n",
    "    canary_model_traffic_percentage='10',\n",
    "    namespace='your_namespace',\n",
    "    framework='tensorflow',\n",
    "    default_custom_model_spec='{}',\n",
    "    canary_custom_model_spec='{}',\n",
    "    autoscaling_target='0',\n",
    "    kfserving_endpoint=''\n",
    "):\n",
    "\n",
    "    # define workflow\n",
    "    kfserving = kfserving_op(action = action,\n",
    "                             model_name=model_name,\n",
    "                             default_model_uri=default_model_uri,\n",
    "                             canary_model_uri=canary_model_uri,\n",
    "                             canary_model_traffic_percentage=canary_model_traffic_percentage,\n",
    "                             namespace=namespace,\n",
    "                             framework=framework,\n",
    "                             default_custom_model_spec=default_custom_model_spec,\n",
    "                             canary_custom_model_spec=canary_custom_model_spec,\n",
    "                             autoscaling_target=autoscaling_target,\n",
    "                             kfserving_endpoint=kfserving_endpoint).set_image_pull_policy('Always')\n",
    "\n",
    "# Compile pipeline\n",
    "import kfp.compiler as compiler\n",
    "compiler.Compiler().compile(kfservingPipeline, 'tf-flower.tar.gz')\n",
    "\n",
    "# Execute pipeline\n",
    "run = client.run_pipeline(experiment.id, 'tf-flower', 'tf-flower.tar.gz')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Custom model example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "kfserving_op = components.load_component_from_url(\n",
    "  'https://raw.githubusercontent.com/kubeflow/pipelines/65bed9b6d1d676ef2d541a970d3edc0aee12400d/components/kubeflow/kfserving/component.yaml'\n",
    ")\n",
    "\n",
    "@dsl.pipeline(\n",
    "  name='kfserving pipeline',\n",
    "  description='A pipeline for kfserving.'\n",
    ")\n",
    "def kfservingPipeline(\n",
    "    action = 'update',\n",
    "    model_name='custom-sample',\n",
    "    default_model_uri='',\n",
    "    canary_model_uri='',\n",
    "    canary_model_traffic_percentage='0',\n",
    "    namespace='kubeflow',\n",
    "    framework='custom',\n",
    "    default_custom_model_spec='{\"name\": \"image-segmenter\", \"image\": \"codait/max-image-segmenter:latest\", \"port\": \"5000\"}',\n",
    "    canary_custom_model_spec='{}',\n",
    "    autoscaling_target='0',\n",
    "    kfserving_endpoint=''\n",
    "):\n",
    "\n",
    "    # define workflow\n",
    "    kfserving = kfserving_op(action = action,\n",
    "                             model_name=model_name,\n",
    "                             default_model_uri=default_model_uri,\n",
    "                             canary_model_uri=canary_model_uri,\n",
    "                             canary_model_traffic_percentage=canary_model_traffic_percentage,\n",
    "                             namespace=namespace,\n",
    "                             framework=framework,\n",
    "                             default_custom_model_spec=default_custom_model_spec,\n",
    "                             canary_custom_model_spec=canary_custom_model_spec,\n",
    "                             autoscaling_target=autoscaling_target,\n",
    "                             kfserving_endpoint=kfserving_endpoint).set_image_pull_policy('Always')\n",
    "\n",
    "# Compile pipeline\n",
    "import kfp.compiler as compiler\n",
    "compiler.Compiler().compile(kfservingPipeline, 'custom.tar.gz')\n",
    "\n",
    "# Execute pipeline\n",
    "run = client.run_pipeline(experiment.id, 'custom-model', 'custom.tar.gz')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}